package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pojo.Goods;
import com.itheima.service.GoodsService;
import com.itheima.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper,Goods> implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品
     * @return
     */
    @Override
    public List<Goods> findAll() {
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = Wrappers.<Goods>lambdaQuery();
        goodsLambdaQueryWrapper.eq(Goods::getIsMarketable,1);
        List<Goods> goods = goodsMapper.selectList(goodsLambdaQueryWrapper);
        return goods;
    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @Override
    public boolean add(Goods goods) {
        goods.setIsMarketable("0");//默认未上架
        goods.setStatus("0");//默认未审核
        goods.setCreatetime(new Date());
        goods.setUpdatetime(new Date());
        goods.setSaleNum(0);
        goods.setCommentNum(0);
        boolean save = this.save(goods);
        return save;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Goods> findPage(Integer page, Integer size) {
        Page<Goods> goodsPage = new Page<>(page, size);
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getIsMarketable,1);
        Page<Goods> page1 = this.page(goodsPage,wrapper);
        List<Goods> records = page1.getRecords();
        return records;
    }

    /**
     * 更新商品
     * @param goods
     * @return
     */
    @Override
    public boolean update(Integer id,Goods goods) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getId,id);
        Integer update = goodsMapper.update(goods, wrapper);
        if(null!=update&&update>0){
            return true;
        }
        return false;
    }

    /**
     * genuineid删除商品
     * @param id
     */
    @Override
    public boolean delete(Integer id) {
        boolean result = this.removeById(id);
        return result;
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Goods findById(Integer id) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        wrapper.eq(Goods::getId,id);
        Goods goods = goodsMapper.selectOne(wrapper);
        return goods;
    }

    /**
     * 上架商品
     * @param id
     * @throws Exception
     */
    @Override
    public void upGoods(Integer id) throws Exception {
        LambdaQueryWrapper<Goods> wrapper1 = Wrappers.<Goods>lambdaQuery();
        wrapper1.eq(Goods::getId,id);
        Goods goods = goodsMapper.selectOne(wrapper1);
        String is_marketable = goods.getIsMarketable();
        if(!"1".equals(is_marketable)){
            throw new RuntimeException("当前商品状态异常,不能进行上架操作");
        }
        LambdaUpdateWrapper<Goods> wrapper2 = Wrappers.<Goods>lambdaUpdate();
        wrapper2.eq(Goods::getId,id);
        goods.setIsMarketable("1");
        goods.setUpdatetime(new Date());
        goodsMapper.update(goods,wrapper2); //更新数据
    }

    /**
     * 下架商品
     * @param id
     * @throws Exception
     */
    @Override
    public void downGoods(Integer id) throws Exception {
        LambdaQueryWrapper<Goods> wrapper1 = Wrappers.<Goods>lambdaQuery();
        wrapper1.eq(Goods::getId,id);
        Goods goods = goodsMapper.selectOne(wrapper1);
        String is_marketable = goods.getIsMarketable();
        if(!"0".equals(is_marketable)){
            throw new RuntimeException("当前商品状态异常,不能进行下架操作");
        }
        LambdaUpdateWrapper<Goods> wrapper2 = Wrappers.<Goods>lambdaUpdate();
        wrapper2.eq(Goods::getId,id);
        goods.setIsMarketable("0");
        goods.setUpdatetime(new Date());
        goodsMapper.update(goods,wrapper2); //更新数据
    }

    /**
     * 更改商品审核状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, String status) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(status);
        goods.setUpdatetime(new Date());
        LambdaUpdateWrapper<Goods> wrapper = Wrappers.<Goods>lambdaUpdate();
        wrapper.eq(Goods::getId,id);
        goodsMapper.update(goods,wrapper);
    }
}




